<?php
class Db_Categoiry extends MVC_SQL {
    public function __construct() {
        $this->setTable('ams_product_category');
    }

    public function doUpdateCategory($aData) {
        if($aData['product_category_yahoo_id']=='') {
            unset($aData['product_category_yahoo_id']);
        }
        return $this->doUpdateByPK($aData);
    }
    public function getInfoCategory($iId) {
        return $this->getRecordsetByPK($iId);
    }
    public function getInfoCategoryByYahooID($YID) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_yahoo_id='".$YID."'";
        return $this->getRecordsetByPKExtend($sSql);
    }
    public function check_exit_category_id($id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_yahoo_id={$id}";
        return $this->getRecordsetExtend($sSql);
    }

    public function getListCategory($oModel) {
        $sSql = "SELECT * FROM ams_product_category WHERE 1 ORDER BY product_category_id ";
        return $this->getRecordsetExtend($sSql);
    }

    public function getListParentCategory($oModel) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id=0 ORDER BY product_category_position asc";

        return $this->getRecordsetExtend($sSql);
    }

    public function getListChildCategory($parent_id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$parent_id} ORDER BY product_category_position asc";
        return $this->getRecordsetExtend($sSql);
    }
    /**
     * CATEGORY CHOOSE
     */
    public function getLastestNumberOfProduct($oModel) {
        $today=date('Y-m-d');
        $sSql="SELECT * FROM ams_product WHERE product_date_added = '{$today}'";

        return $this->getNumRow($sSql);
    }
    public function getListParentCategoryChoose($oModel) {
        $sSql="SELECT * FROM ams_product_category WHERE product_category_parent_id=0 AND product_category_status=1 ORDER BY product_category_name";
        return $this->getRecordsetExtend($sSql);
    }

    public function getListChildCategoryChoose($parent_id) {
        $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$parent_id} AND product_category_status=1";
        return $this->getRecordsetExtend($sSql);
    }
    public function doInsertGenCategoryId($gen_pro_id,$cat_id) {
        $sSql="INSERT INTO ams_product (product_id,product_category_id,product_date_added) values ($gen_pro_id,$cat_id,'".date('Y-m-d')."')";

        return $this->doUpdateExtend($sSql);
    }

    //
    public function doUpdateAllChildStatus($cat_id,$status) {
        $sSql="UPDATE ams_product_category set product_category_status={$status} WHERE product_category_parent_id = {$cat_id}";
        return $this->doUpdateExtend($sSql);
    }
    function  updatePosition () {
    //        print_R($_POST['position']);
        foreach(array_keys($_POST['position']) as $id):
            $position= $_POST['position'][$id];
            $sql="update `ams_product_category` set `product_category_position`='$position'  where `product_category_id`='$id'";

            $this->doUpdateExtend($sql);
        endforeach;

    }

}
